/**
 * 所有指令都写在这里
 * Created by 87487 on 2017/7/6.
 */
/* eslint-disable */
export default (Vue) => {
    return Vue.directive('checkboxAlign', {
        bind(el, binding, vnode) {
            //监听数据变化
            let alignFn = () => {
                let width = 0;
                let _this = vnode.componentInstance;
                _this.$nextTick(() => {
                    //获取宽度
                    let $list = el.querySelectorAll('.el-checkbox');
                    for(let i = 0; i < $list.length; i++){
                        let item = $list[i];
                        width = item.clientWidth > width ? item.clientWidth : width;
                    }
                    //设置宽度
                    for(let i = 0; i < $list.length; i++){
                        let item = $list[i];
                        item.style.width = width + 'px';
                        if(i === $list.length - 1){
                            item.style.marginRight = '30px';
                        }
                    }
                });
            }

            if(binding.rawName.indexOf(':') > 0) {
                let name = binding.rawName.split(':')[1];
                let datas = name.split('.');
                vnode.context.$watch(() => {
                    let data = vnode.context;
                    for(let i = 0; i < datas.length; i++){
                        data = data[datas[i]];
                    }
                    return data;
                }, (newVal, oldVal) => {
                    alignFn();
                }, {
                    deep:true
                })
            } else {
                alignFn();
            }
        }
    })
}
